﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy1_9
{
    public partial class MainForm : Form
    {
        private readonly Size balloonInitialSize = new Size(120, 120);
        private readonly int moveStep = 5;
        private readonly int shrinkStep = 2;
        private bool isAscending = false;

        public MainForm()
        {
            InitializeComponent();
            Initial();
        }

        private void Initial()
        {
            timerBalloon.Stop();
            picBalloon.Size = balloonInitialSize;
            picBalloon.Left = 0;
            picBalloon.Top = this.ClientSize.Height - picBalloon.Height;
            btnStart.Text = "开始";
            isAscending = false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (timerBalloon.Enabled)
            {
                timerBalloon.Stop();
                btnStart.Text = "开始";
                isAscending = false;
            }
            else
            {
                timerBalloon.Start();
                btnStart.Text = "停止";
                isAscending = true;
            }
        }

        private void timerBalloon_Tick(object sender, EventArgs e)
        {
            if (picBalloon.Top <= 0)
            {
                timerBalloon.Stop();
                btnStart.Text = "开始";
                isAscending = false;

                MessageBox.Show("气球已飞到顶部！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                ResetBalloonPosition();
                return;
            }

            picBalloon.Left += moveStep;
            picBalloon.Top -= moveStep;

            if (isAscending && picBalloon.Width > shrinkStep && picBalloon.Height > shrinkStep)
            {
                picBalloon.Width -= shrinkStep;
                picBalloon.Height -= shrinkStep;
            }
        }

        private void ResetBalloonPosition()
        {
            picBalloon.Size = balloonInitialSize;
            picBalloon.Left = 0;
            picBalloon.Top = this.ClientSize.Height - picBalloon.Height;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            picBalloon.BackColor = Color.Transparent;
            this.DoubleBuffered = true;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (!timerBalloon.Enabled)
            {
                picBalloon.Top = this.ClientSize.Height - picBalloon.Height;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Initial();
        }
    }
}